package net.hands.ado.db.jdbc;

import java.sql.ResultSet;

import javax.sql.DataSource;

import net.hands.ado.db.SQLValue;
import net.hands.ado.db.jdbc.dialect.Dialect;
import net.hands.ado.db.jdbc.dialect.MySQLDialect;

/**
 * 这是一个开源的软件，请在LGPLv3下合法使用、修改或重新发布。
 * 
 * @author 陈侃(cknet@126.com, 13910090885)
 *         http://code.google.com/p/simpleframework/
 *         http://www.simpleframework.net
 */
public abstract class AbstractJdbcProvider implements IJdbcProvider {
	/**
	 * 数据源
	 */
	private final DataSource dataSource;

	public AbstractJdbcProvider(final DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public DataSource getDataSource() {
		return dataSource;
	}

	@Override
	public void doQuery(final SQLValue sqlVal, final IQueryCallback callback) {
		doQuery(sqlVal, callback, ResultSet.TYPE_FORWARD_ONLY,
				ResultSet.CONCUR_READ_ONLY);
	}

	@Override
	public DatabaseMeta getDatabaseMeta() {
		return JdbcUtils.getDatabaseMetaData(getDataSource());
	}

	/**
	 * 创建方言
	 * 
	 * @return
	 */
	public Dialect createJdbcDialect() {
		final DatabaseMeta meta = getDatabaseMeta();
		if (meta.isMySql()) {
			return new MySQLDialect();
		}
		// else if (meta.isOracle()) {
		// return new OracleJdbcDialect();
		// } else if (meta.isMSSql()) {
		// return new SqlServerJdbcDialect();
		// } else if (meta.isHSql()) {
		// return new HSQLJdbcDialect();
		// } else if (meta.isPostgreSql()) {
		// return new PostgresqlJdbcDialect();
		// } else {
		// return new DefaultJdbcDialect();
		// }
		return null;
	}

	private Dialect jdbcDialect;

	@Override
	public Dialect getJdbcDialect() {
		if (jdbcDialect == null) {
			jdbcDialect = createJdbcDialect();
		}
		return jdbcDialect;
	}
}
